package com.lxj.domain;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * @author lxj
 * @date 2021/7/30 16:15
 * @Description:
 */
@ApiModel(value = "com-lxj-com.lxj.domain-SysUser")
@Data
@AllArgsConstructor
@NoArgsConstructor
@TableName(value = "sys_user")
public class SysLoginUser implements UserDetails {

    @TableId(value = "user_id", type = IdType.AUTO)
    @ApiModelProperty(value = "")
    private Long userId;

    /**
     * 用户名
     */
    @TableField(value = "username")
    @ApiModelProperty(value = "用户名")
    private String username;

    /**
     * 密码
     */
    @TableField(value = "password")
    @ApiModelProperty(value = "密码")
    private String password;

    /**
     * 状态  0：禁用   1：正常
     */
    @TableField(value = "status")
    @ApiModelProperty(value = "状态  0：禁用   1：正常")
    private Integer status;


    /**
     * 用户所在的商城Id
     */
    @TableField(value = "shop_id")
    @ApiModelProperty(value = "用户所在的商城Id")
    private Long shopId;


    @TableField(exist = false)
    @ApiModelProperty(value = "后台用户的权限集合")
    private List<String> auths;

    /**
     * 权限
     *
     * @return
     */
    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        ArrayList<GrantedAuthority> grantedAuthorities = new ArrayList<>();
        auths.forEach(auth -> {
            if (StringUtils.hasText(auth)) {
                // 分割
                if (auth.contains(",")) {
                    String[] realAuths = auth.split(",");
                    // 循环
                    for (String realAuth : realAuths) {
                        grantedAuthorities.add(new SimpleGrantedAuthority(realAuth));
                    }
                }else {
                    grantedAuthorities.add(new SimpleGrantedAuthority(auth));
                }
            }
        });
        return grantedAuthorities;
    }

    @Override
    public String getPassword() {
        return this.password;
    }

    /**
     * 使用id
     *
     * @return
     */
    @Override
    public String getUsername() {
        return String.valueOf(this.userId);
    }

    @Override
    public boolean isAccountNonExpired() {
        return this.status == 1;
    }

    @Override
    public boolean isAccountNonLocked() {
        return this.status == 1;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return this.status == 1;
    }

    @Override
    public boolean isEnabled() {
        return this.status == 1;
    }
}
